// OnStyle
//
// Copyright (c)  Daigo Tanaka
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

onStyle.main = function(){
var private = {};
private.public = this; // need for accessing public method from private

var common = new onStyle.common();
var elementExpert = new onStyle.elements(this);
var output = new onStyle.output(this);
var events = new onStyle.events(this);
var designer = new onStyle.designer(this);
var editor = new onStyle.editor(this);
var dialog = null;
var container = null;

var initializedOnce = false;

this.browserSupported = function()
{
  return true;

  if (navigator.appName == "Microsoft Internet Explorer"){
    alert("OnStyle currently does not support Internet Explorer :(");
    return false;
  }
  return true;
};

this.getRootContainer = function()
{
  return container;
}


this.getEvents = function()
{
  return events;
}

this.getElementExpert = function()
{
  return elementExpert;
};

this.getCommon = function()
{
  return common;
}

this.getDialog = function()
{
  return dialog;
}

this.getEditor = function()
{
  return editor;
}

this.getDesigner = function()
{
  return designer;
}

this.getOutput = function()
{
  return output;
}

// Set default value to the root container, and ask the user to start
this.start = function()
{
  output.createOutputArea();
  private.populateRootOptions();
  alert("Set the root continer ID and click \"set\" to begin.");
  private.initializeDialog();
};

this.changeRoot = function()
{
  newRootID = document.getElementById(onStyle.RootContainerID).value;
  if (newRootID){
    newContainer = document.getElementById(newRootID);
  }
  if (newContainer){
    var goahead = true;
    if (initializedOnce == true){
      goahead = confirm("Make sure you save the current edit before change root! Continue?");
    }
    if (goahead){
      private.initOnStyle(newRootID, false);
      return;
    }    
  }
  else{
    alert("Could not find such ID");
  }
  if (container){
    display = document.getElementById(onStyle.RootContainerID);
    display.value = container.id;
  }
};



private.populateRootOptions = function()
{
  var select = document.getElementById(onStyle.RootContainerID);
  if (!select){
    alert("UI error. Please contact the developer.");
    return;
  }

  var elements = document.getElementsByTagName("DIV");
  if (!elements) return false;
  for (elementIndex = 0; elementIndex < elements.length; elementIndex++){
    if (elements[elementIndex].id && elementExpert.isEditable(elements[elementIndex])){
      // Note: IE7 has a bug of not being able to edit innerHTML of select so do this:
      select.options[select.options.length] = new Option(elements[elementIndex].id, elements[elementIndex].id);
    }
  }  
};

private.initializeDialog = function()
{
  if (dialog){
    events.removeEvent(dialog, "mousemove", events.update, false);
    events.removeEvent(dialog, "mousedown", events.onMouseDown, false);
    dialog = null;
  }

  dialog = document.getElementById(onStyle.DialogID);
  if (!dialog){
    alert("Initialization error: Dialog box not found.");
    return;
  }

  select = document.getElementById(onStyle.RootContainerID);
  select.focus();

  events.update();
};

private.initOnStyle = function(elementID, findAlternativeDiv)
{
  if (!this.public.browserSupported()) return;

  var eventRoot = events.getEventRoot();
  if (eventRoot){
    events.removeEvent(eventRoot, "mousemove", events.update, false);
    events.removeEvent(eventRoot, "mousedown", events.onMouseDown, false);
    events.removeEvent(eventRoot, "mouseover", events.onMouseOver, false);
    events.removeEvent(eventRoot, "mouseout", events.onMouseOut, false);
  }

  container = null;
  if (elementID){
    container = document.getElementById(elementID);
  }
  if (!container && findAlternativeDiv)
  {
    var elementList = document.getElementsByTagName('div');
    if (elementList.length > 0){
      container = elementList[0];
    }
  }
  if (!container){
    alert("Please set a correct root container ID from the dialog.");
    return;
  }

  events.setEventRoot(container);
  eventRoot = events.getEventRoot();
  container.style.zIndex = 0;
  
  display = document.getElementById(onStyle.RootContainerID);
  display.value = container.id;

  elementExpert.initializeElement(container);
  if (container.hasChildNodes()){
    var children = container.childNodes;
    for (var childIndex = 0; childIndex < children.length; childIndex++){
      if (children[childIndex].nodeName == "DIV" &&  elementExpert.isEditable( children[childIndex] )){
        elementExpert.initializeElement(children[childIndex]);
      }
    }
  }

  if (container.style){
    container.style.position = "relative";
    events.setMouseOffset( parseInt(container.style.left),
      parseInt(container.style.top) );
    container.style.top = "0px";
    container.style.left = "0px";
    container.style.width = designer.getTotalWidth(container) + "px";
    container.style.height = designer.getTotalHeight(container) + "px";
  }

  events.addEvent(eventRoot, "mousemove", events.update, false);
  events.addEvent(eventRoot, "mousedown", events.onMouseDown, false);
  events.addEvent(eventRoot, "mouseover", events.onMouseOver, false);
  events.addEvent(eventRoot, "mouseout", events.onMouseOut, false);

  events.addEvent(dialog, "mousemove", events.update, false);
  events.addEvent(dialog, "mousedown", events.onMouseDown, false);
  events.addEvent(dialog, "mouseover", events.onMouseOver, false);
  events.addEvent(dialog, "mouseout", events.onMouseOut, false);

  events.update();

  initializedOnce = true;

  designer.dialog = document.getElementById(onStyle.DialogID);
  designer.dialog.style.backgroundColor = "#CCCCCC";
  var console = document.getElementById(onStyle.EditorID);
  console.innerHTML = "OnStyle initialized with root container:" + container.id;
};

};

onStyle.instances.main = new onStyle.main();
onStyle.instances.events = onStyle.instances.main.getEvents(); 
onStyle.instances.main.start();

